package com.tm.media.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.utils.utils.Result;
import com.tm.media.utils.utils.ResultCodeEnum;
import com.tm.media.login.service.UserLoginService;
import com.tm.media.model.entity.AdUser;
import com.tm.media.model.mapper.UserLoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, AdUser> implements UserLoginService {
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<AdUser> selectUser() {
        List<AdUser> userEntities = userLoginMapper.selectList(null);
        return userEntities;
    }

    @Override
    public int insertUser(AdUser userEntity) {
        return 0;
    }

    @Override
    public Result login(String yanzhengma, String name, String password, HttpServletRequest request) {
     /*   String ip = request.getRemoteAddr();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String o = (String) valueOperations.get("yanzhengma" + ip.replaceAll(":", ""));*/
        //if(yanzhengma.equals(o)){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        AdUser userEntity = userLoginMapper.selectOne(queryWrapper);
        if (userEntity != null) {
            if (userEntity.getPassword().equals(password)) {
                return Result.SUCCESS(ResultCodeEnum.LOGIN_SUCCESS);
            } else {
                return Result.FAIL(ResultCodeEnum.LOGIN_FAIL);
            }
           /* }else{
                return Result.FAIL(ResultCodeEnum.LOGIN_FAIL);
            }
        }else{
            return Result.FAIL(ResultCodeEnum.CODE_FAIL);
        }*/
        }return Result.FAIL(500,"不能为空");
    }
}
